<?php
/**
 * 
 * @author tanaka
 * @package 
 * @subpackage
 */
class Db_Master_Job extends Db_TableAbstract
{
	const TABLE_NAME = 'm_job';
	
	const MAJOR_CATEGORY_MINOR_CD = 0;
	
	private static $majorJobs;
	private static $minorJobs;
	
	/**
	 * すべてのレコードを返すセレクタ
	 * 
	 * @return Zend_Db_Select
	 */
	public function selectAll()
	{
		return $this->_db->select()->from(self::TABLE_NAME);
	}
	
	/**
	 * すべての大分類を返すセレクタ
	 * 
	 * @return Zend_Db_Select
	 */
	public function selectAllMajor()
	{
		return $this->selectAll()->where('JOB_MINOR_CD = ?', self::MAJOR_CATEGORY_MINOR_CD);
	}
	
	/**
	 * すべての中分類を返すセレクタ
	 *
	 * @return Zend_Db_Select
	 */
	public function selectAllMinor()
	{
		return $this->selectAll()->where('JOB_MINOR_CD != ?', self::MAJOR_CATEGORY_MINOR_CD);
	}
	
	/**
	 * 大分類名を返す
	 * @param string $majorCd
	 * @return string
	 */
	public function getMajor($majorCd)
	{
		$allMajor = $this->getAllMajorStructed();
		
		return isset($allMajor[$majorCd]) ? $allMajor[$majorCd] : '';
	}
	
	public function getMinor($majorCd, $minorCd)
	{
		$allMinor = $this->getAllMinorByMajorCd($majorCd);
		return isset($allMinor[$minorCd]) ? $allMinor[$minorCd] : '';
	}
	
	/**
	 * すべての大分類を返す
	 * 
	 * @return array
	 */
	public function getAllMajor()
	{
		if(is_null(self::$majorJobs)){
			self::$majorJobs = $this->_db->fetchAll($this->selectAllMajor());
		}
		
		return self::$majorJobs;
	}
	
	/**
	 * すべての中分類を返す
	 * 
	 * @return array
	 */
	public function getAllMinor()
	{
		if(is_null(self::$minorJobs)){
			self::$minorJobs = $this->_db->fetchAll($this->selectAllMinor());
		}
		
		return self::$minorJobs;
	}
	
	/**
	* すべての中分類を大分類ごとの配列にまとめて返す
	*
	* @return array
	*/
	public function getAllMajorStructed()
	{
		$result = array();
		
		foreach($this->getAllMajor() as $row){
			$result[$row['JOB_MAJOR_CD']] = $row['JOB_NAME'];
		}
		
		return $result;
	}
	
	/**
	 * すべての中分類を大分類ごとの配列にまとめて返す
	 * 
	 * @return array
	 */
	public function getAllMinorStructed()
	{
		$result = array();
		
		foreach($this->getAllMinor() as $row){
			
			if(!isset($result[$row['JOB_MAJOR_CD']])){
				$result[$row['JOB_MAJOR_CD']] = array();
			}
			
			$result[$row['JOB_MAJOR_CD']][$row['JOB_MINOR_CD']] = $row['JOB_NAME'];
		}
		
		return $result;
	}
	
	/**
	 * 大分類からすべての小分類を返す
	 * @param int $majorCd
	 * @return array
	 */
	public function getAllMinorByMajorCd($majorCd)
	{
		$minors = $this->getAllMinorStructed();
		return isset($minors[$majorCd]) ? $minors[$majorCd] : array();
	}
}